
using System;
using System.Windows.Forms;
using SqlRunner;
using SQLRunner.uiStatus;
using SQLRunnerLib.RunnableItems;
using SQLServices.RunnableItems;

namespace SQLRunner.gui
{
    partial class frmMain:IScriptsView
    {

        #region Properties
        
        internal State FormState
        {
            get
            {
                return _formState;
            }
            set
            {
                _formState = value;
                _formPropertyStateManager.UpdateStatus(value);
            }
        }

        #endregion

        #region Private Methods

        private void SetGroups()
        {
            // Indicate which controls are enabled at the Initial state
            PropertyStateList g = new PropertyStateList("Enabled", State.Initial);
            g.AddControl(grpLogin);
            _formPropertyStateManager.AddGroup(g);

            g = new PropertyStateList("Enabled", State.Connected);
            g.AddControl(grpDB);
            _formPropertyStateManager.AddGroup(g);

            g = new PropertyStateList("Enabled", State.DBSelected);
            g.AddControl(grpConnMgr);

        }

        private TreeNode AddFolder(ProjectFolder projectFolder, TreeNode parentNode)
        {
            TreeNodeCollection nodes = parentNode == null ? fileTreeViewer.Nodes : parentNode.Nodes;
            TreeNode node = nodes.Add(
                projectFolder.Path.FullPath, 
                projectFolder.Name, 
                IconList.Folder.ToString(), 
                IconList.Folder.ToString());
            node.Checked = projectFolder.Selected;         
            foreach (IProjectItem child in projectFolder.Children)
            {            
                if (child is ProjectFolder)
                {
                    TreeNode subNode = AddFolder((ProjectFolder) child, node);
                    subNode.Checked = child.Selected;
                }
                else
                {
                    TreeNode scriptNode = AddChildToFolder(node, child as ProjectScript);
                    if (scriptNode != null) scriptNode.Checked = child.Selected;
                }
            }
            return node;
        }

        private TreeNode AddChildToFolder(TreeNode node, ProjectScript script)
        {
            if (script == null) return null;
            TreeNode scriptNode = null;
            if (script.Path.FullPath != null)
            {
                scriptNode = node.Nodes.Add(script.Path.FullPath, script.Name, IconList.File.ToString(), IconList.File.ToString());
            }
            return scriptNode;
        }

        #endregion

        private ScriptsController scriptController;

        public void DisplayView()
        {
            Show();
        }

        public ScriptsController Controller
        {
            get { return scriptController; }
            set { scriptController = value; }
        }

        public void Update(ScriptsModel scriptsModel, EventArgs e)
        {
            fileTreeViewer.SuspendLayout();
            fileTreeViewer.Visible = false;
            fileTreeViewer.Nodes.Clear();
            foreach (ProjectFolder folder in scriptsModel.ProjectMgr.RootFolders )
            {
                AddFolder(folder, null);
            }
            fileTreeViewer.Visible = true;
            fileTreeViewer.ResumeLayout(true);
        }

        public void Subscribe(ScriptsModel scriptsModel)
        {
            scriptsModel.ModelChange += Update;
        }

    }
}
